package com.shwread.android.qysw.tools;


import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeHelper {


    Calendar mCalendar;

    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd");

    public static final SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm");
    public static final SimpleDateFormat DATE_FORMAT_y_m_d_h_m_s = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_m_d_h_m = new SimpleDateFormat("MM月dd日 HH:mm");
    public static final SimpleDateFormat DATE_FORMAT_m_d = new SimpleDateFormat("MM/dd");
    public static final SimpleDateFormat jsonDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat signCardDateFormat = new SimpleDateFormat("yyyy.MM.dd | HH:mm");
    public static final SimpleDateFormat RecordWeekStartFormat = new SimpleDateFormat("yyyy年MM月dd日");
    public static final SimpleDateFormat RecordWeekEndFormat = new SimpleDateFormat("MM月dd日");


    private static class TimeHelperHolder {
        public final static TimeHelper sington = new TimeHelper();
    }

    public TimeHelper() {
        mCalendar = Calendar.getInstance();
    }


    public static String getDay(Date created_time) {
        return TimeHelper.dateFormat.format(created_time);
    }


    public static String getMyDay(Date created_time) {
        return TimeHelper.RecordWeekStartFormat.format(created_time);
    }
    public static int getWeeKDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK) - 1;
    }

    public boolean isThisYear(Date date) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(new Date());
        c2.setTime(date);

        return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR);
    }

    public static int getNowMonth() {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());
        Log.i("darren", "MONTH:" + Calendar.MONTH);
        Log.i("darren", "MONTH1:" + c1.get(Calendar.MONTH));
        return c1.get(Calendar.MONTH) + 1;
    }

    public static String getDays(long t1, long t2) {
        long day = 0;
        day = (t2 - t1) / (24 * 60 * 60 * 1000);
        day = day + 1;
        return "(" + day + "天)";

    }

    public static List<Date> getLastWeekDays() {
        Date mdate = new Date();
        Date fdate;
        List<Date> list = new ArrayList();
        Long fTime = mdate.getTime() - 6 * 24 * 3600000;
        for (int a = 0; a < 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            Log.i("darren", "day" + a + ":" + TimeHelper.getDayNum(fdate));
            list.add(a, fdate);
        }
        return list;
    }

    public static List<String> getLastWeekDaysString() {
        Date mdate = new Date();
        Date fdate;
        List<String> list = new ArrayList();

        Long fTime = mdate.getTime() - 6 * 24 * 3600000;
        for (int a = 0; a < 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            Log.i("darren", "day" + a + ":" + TimeHelper.getDayNum(fdate));
            list.add(a, TimeHelper.getDayNum(fdate) + "");
        }
        return list;
    }

    public static List<Date> getLastServalDays(int days) {
        Date mdate = new Date();
        Date fdate;
        List<Date> list = new ArrayList();

        Long fTime = mdate.getTime();
        for (int a = 0; a <days; a++) {
            fdate = new Date();
            fdate.setTime(fTime - (a * 24 * 3600000));
            Log.i("darren", "day" + a + ":" + TimeHelper.getDayNum(fdate));
            list.add(a, fdate);
        }
        return list;
    }

    public static List<String> getMonthsString(int nowMonth) {
        List<String> list = new ArrayList();
        for (int i = 0; i < 11; i++) {
            int month = (nowMonth + i + 1) % 12;
            if (month == 0) {
                month = 12;
            }
            list.add(i, month + "");
        }
        return list;
    }

    public static int getDayNum(Date date) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
//        Log.i("darren", "date:" + Calendar.DATE);
//        Log.i("darren", "date1:" + c1.get(Calendar.DATE));
        return c1.get(Calendar.DATE);
    }

    public static int getToday() {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());
        Log.i("darren", "date:" + Calendar.DATE);
        Log.i("darren", "date1:" + c1.get(Calendar.DATE));
        return c1.get(Calendar.DATE);
    }

    public String formatDateForTitle(Date created_time) {
        if (isThisYear(created_time)) {
            return TimeHelper.RecordWeekEndFormat.format(created_time);
        } else {
            return TimeHelper.RecordWeekStartFormat.format(created_time);
        }
    }

    public void refresh() {
        mCalendar = Calendar.getInstance();
    }


    public static TimeHelper getInstance() {
        return TimeHelperHolder.sington;
    }


    public String getTodayString() {
        refresh();
        return dateFormat.format(mCalendar.getTime());
    }

    public String getSevenDayLaterDayString() {
        refresh();
        Calendar calendar = (Calendar) this.mCalendar.clone();
        calendar.add(Calendar.DAY_OF_MONTH, 6);
        return dateFormat.format(calendar.getTime());
    }

    public static String getTimeDifferenceString(Date date) {
        Calendar mCalendar = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        if (mCalendar.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)) {
            return hourFormat.format(date);
        } else {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            if (mCalendar.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)) {
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                return "昨天 " + hourFormat.format(date);
            } else {
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                return simpleDateFormat.format(calendar.getTime()) + " " + hourFormat.format(date);
            }
        }
    }

    public static String getNewTimeDifferenceString(Date date) {//打卡日期新规则
        Calendar mCalendar = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        long l = mCalendar.getTimeInMillis() - date.getTime();
        if (mCalendar.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)) {
            int hour = (int) (l / (60 * 60 * 1000));
            if (hour < 1) {
                return "刚刚";
            } else if (1 <= hour && hour <= 4) {
                return hour + "小时前";
            } else {
                return "今天";
            }
        } else {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            if (mCalendar.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)) {
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                return "昨天 ";
            } else {
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                return simpleDateFormat.format(calendar.getTime());
            }
        }
    }


    public static int compare(Date d1, Date d2) {
        return (int) (d1.getTime() - d2.getTime());
    }

    public static List<String> getBetweenDateList(Date d1, Date d2) {
        List<String> dateList = new ArrayList<>();
        long from = d1.getTime();
        long to = d2.getTime();
        int day = (int) ((to - from) / (1000 * 60 * 60 * 24));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        dateList.add(format.format(d1));
        for (int i = 0; i < day; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date date1 = new Date(calendar.getTimeInMillis());
            dateList.add(format.format(date1));
        }
        return dateList;
    }

    public static List<String> getBetweenDateList(Date d1, int days) {
        List<String> dateList = new ArrayList<>();
        long from = d1.getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        dateList.add(format.format(d1));
        for (int i = 0; i < days; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date date1 = new Date(calendar.getTimeInMillis());
            dateList.add(format.format(date1));
        }
        return dateList;
    }

    public static String getTheDayTomorrowDay(String d1) {
        String tomorrow = null;
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = fmt.parse(d1);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date date1 = new Date(calendar.getTimeInMillis());
            tomorrow = format.format(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return tomorrow;
    }

    public static List<String> getForwardBetweenDateList(Date d1, int days) {
        List<String> dateList = new ArrayList<>();
        long from = d1.getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        dateList.add(format.format(d1));
        for (int i = 0; i < days; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            Date date1 = new Date(calendar.getTimeInMillis());
            dateList.add(format.format(date1));
        }
        return dateList;
    }

    public static boolean isToToday(String date) {
        Date date1 = null;
        try {
            date1 = TimeHelper.dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1.getTime() >= Calendar.getInstance().getTime().getTime();
    }

    public static int getBetweenDaysNumber(String dateTo, String dateFrom) {
        Date date_to = null;
        Date date_from = null;
        try {
            date_to = TimeHelper.dateFormat.parse(dateTo);
            date_from = TimeHelper.dateFormat.parse(dateFrom);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (int) ((date_to.getTime() - date_from.getTime()) / (1000 * 60 * 60 * 24)) + 1;
    }


    // a integer to xx:xx:xx
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Long.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    //返回 3/21
    public static String getDate_M_DByTimeStamp(long time) {
        Date d = new Date(time);
        return DATE_FORMAT_m_d.format(d);
    }

    public static String getDateY_M_D_TimByStamp(long time) {
        Date d = new Date(time);
        return dateFormat.format(d);
    }

    //返回 3月21日
    public static String getMDate_M_DByTimeStamp(long time) {
        Date d = new Date(time);
        return RecordWeekEndFormat.format(d);
    }

    public static String getFriendPKDetailTime(long time) {
        String res = "";
        long nowTime = System.currentTimeMillis();
        long l = time - nowTime;
        if (l > 0) {
            long hour = (l / (60 * 60 * 1000));
            if (hour > 0) {
                res = res + unitFormat(hour) + ":";
            }
            long min = ((l / (60 * 1000)) - hour * 60);
            if (min >= 0) {
                res = res + unitFormat(min) + ":";
            }
            long s = (l / 1000 - hour * 60 * 60 - min * 60);
            if (s >= 0) {
                res = res + unitFormat(s);
            }
        } else {
            res = "";
        }

//
//        long hour = 0;
//        long minute = 0;
//        long second = 0;
//        if (l <= 0)
//            return "00:00";
//        else {
//           minute = l / 60;
//            if (minute < 60) {
//                second = time % 60;
//                res = unitFormat(minute) + ":" + unitFormat(second);
//            } else {
//                hour = minute / 60;
//                if (hour > 99)
//                    return "99:59:59";
//                minute = minute % 60;
//                second = l - hour * 3600 - minute * 60;
//                res = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
//            }
//        }


        return res;
    }

    //1天2时5分3秒
    public static String getMyCountDownTime(long time) {
        String res = "";
        long nowTime = System.currentTimeMillis();
        long l = time - nowTime;
        if (l > 0) {
            long day = l / (24 * 60 * 60 * 1000);
            if (day > 0) {
                res = res + day + "天";
            }

            long hour = (l / (60 * 60 * 1000) - day * 24);
            if (hour > 0) {
                res = res + hour + "小时";
            }
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            if (min > 0) {
                res = res + min + "分";
            }
//            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
//            if (s > 0) {
//                res = res + s + "秒";
//            }
        } else {
            res = "";
        }
        return res;
    }

    //return 03:02:33
    public static String getMySecCountDownTime(long time) {
        String res = "";
        long nowTime = System.currentTimeMillis();
        long l = time - nowTime;
        if (l > 0) {
            long day = l / (24 * 60 * 60 * 1000);
            if (day > 0) {
                res = res + day + ":";
            }

            long hour = (l / (60 * 60 * 1000) - day * 24);
            if (hour > 0) {
                res = res + hour + ":";
            }
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            if (min > 0) {
                res = res + min + ":";
            }
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            if (s > 0) {
                res = res + s;
            }
        } else {
            res = "已开始";
        }
        return res;
    }

    //1天2时5分3秒
    public static String getMyFriendCountDownTime(long time) {
        String res = "";
        long nowTime = System.currentTimeMillis();
        long l = time - nowTime;
        if (l > 0) {
            long hour = (l / (60 * 60 * 1000));
            if (hour > 0) {
                res = res + hour + "小时";
            }
            long min = ((l / (60 * 1000)) - hour * 60);
            if (min >= 0) {
                res = res + min + "分";
            }
            long s = (l / 1000 - hour * 60 * 60 - min * 60);
            if (s >= 0) {
                res = res + s + "秒";
            }
        } else {
            res = "已开始";
        }
        return res;
    }

    //友谊赛判断今天还是明天
    public static boolean isToday(long time) {
        Date date = new Date(time);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(new Date());
        c2.setTime(date);

        return c1.get(Calendar.DATE) == c2.get(Calendar.DATE);
    }

    //获取凌晨时间+10s
    public static long getMyFriendPKStart() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        Log.i("darren", "hour" + hour);
        long myDay = 24 * 60 * 60 * 1000;
        if (hour >= 12) {
            return System.currentTimeMillis() - millisecond + myDay + 10 * 1000;
        } else {
            return System.currentTimeMillis() - millisecond + myDay / 2 - 10 * 1000;
        }

    }

    public static long getTodayPKEnd() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        long myDay = 24 * 60 * 60 * 1000;
        return System.currentTimeMillis() - millisecond + myDay - 10 * 1000;
    }

    public static long getTomorrowPKEnd() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        long myDay = 48 * 60 * 60 * 1000;
        return System.currentTimeMillis() - millisecond + myDay - 10 * 1000;
    }

    //-10s
    public static long getMyFriendPKEnd(long startT) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        long myDay;
        if (hour >= 12) {
            myDay = 24 * 60 * 60 * 1000 - 20 * 1000;
        } else {
            myDay = 12 * 60 * 60 * 1000 - 10 * 1000;
        }
        return startT + myDay;
    }

    public static Date weeHours(Date date, int flag) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        //时分秒（毫秒数）
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        //凌晨00:00:00
        cal.setTimeInMillis(cal.getTimeInMillis() - millisecond);

        if (flag == 0) {
            return cal.getTime();
        } else if (flag == 1) {
            //凌晨23:59:59
            cal.setTimeInMillis(cal.getTimeInMillis() + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000);
        }
        return cal.getTime();
    }

    public static long getStartTime(String s) throws ParseException {
        Date date = jsonDateFormat.parse(s + " 00:00:10");
        Log.i("darren", "startD:" + s + " 00:00:10");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTimeInMillis();
    }

    public static long getTodayStartTime(String s) throws ParseException {
        Date date = jsonDateFormat.parse(s + " 12:00:10");
        Log.i("darren", "startD:" + s + " 12:00:10");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTimeInMillis();
    }

    public static long getEndTime(String s) throws ParseException {
        Date date = jsonDateFormat.parse(s + " 23:59:50");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTimeInMillis();
    }

    public static long getGroupGameStartTime(String s) throws ParseException {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour >= 12) {
            Date date = jsonDateFormat.parse(s + " 00:00:10");
            Log.i("darren", "startD:" + s + " 00:00:10");
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date);
//            return cal2.get
        } else {

        }


        return cal.getTimeInMillis();
    }

    public static boolean isBerfore12Clock() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour < 12) {
            return true;
        } else {
            return false;
        }
    }

    public static long getTodayTime() {
        Date date = new Date();
        String x = dateFormat.format(date);
        Date date1 = null;
        try {
            date1 = jsonDateFormat.parse(x + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        return cal.getTimeInMillis();
    }

    public final static class TimeTableDayItem {
        private int week;
        private String weekString;
        private int day;
        private boolean canSelect;
        private Date date;
        private boolean enabled = true;

        public Date getDate() {
            return date;
        }

        public void setDate(Date date) {
            this.date = date;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public int getWeek() {
            return week;
        }

        public void setWeek(int week) {
            this.week = week;
        }

        public String getWeekString() {
            return weekString;
        }

        public void setWeekString(String weekString) {
            this.weekString = weekString;
        }

        public int getDay() {
            return day;
        }

        public void setDay(int day) {
            this.day = day;
        }

        public boolean isCanSelect() {
            return canSelect;
        }

        public void setCanSelect(boolean canSelect) {
            this.canSelect = canSelect;
        }
    }


}
